package com.example.adminuser.controller;

import com.example.adminuser.dto.AdminUserPage;
import com.example.adminuser.entity.AdminUser;
import com.example.adminuser.service.AdminUserService;
import com.example.goods.entity.Goods;
import com.example.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 13:49 2019/4/4
 */
@Controller
@RequestMapping("/admin")
public class AdminUserController {
    @Autowired
    @Qualifier("adminUserServiceImpl")
    private AdminUserService adminUserService;

    @Autowired
    @Qualifier("goodsServiceImpl")
    private GoodsService goodsService;

    @RequestMapping("/login")
    public String adminLogin(){
        return "admin/login";
    }
    @RequestMapping("/login/check")
    @ResponseBody
    public String loginCheck(@RequestBody AdminUser adminUser, HttpSession session){
        AdminUser currentUser=adminUserService.adminUserLogin(adminUser);
        if (currentUser==null){
            return "login_fail";
        }else {
            session.setAttribute("adminuser",currentUser);
            return "login_succ";
        }
    }

    @RequestMapping("/userinfo")
    @ResponseBody
    public AdminUser getUserInfo(HttpSession session){
    AdminUser adminUser= (AdminUser) session.getAttribute("adminuser");
    adminUser.setAdminUserAvatar("/images/avatar.png");
     return adminUser;
    }
    @RequestMapping("/home")
    public String adminHome(){
     return "admin/home";
    }
    @RequestMapping("/goodlist")
    public String adminGoodslist(){
      return "admin/goodsList";
    }

    @RequestMapping("/publishgoods")
    @ResponseBody
    public String publistGoods(@RequestBody Goods goods){
        goodsService.publishById(goods);
        return "succ";
    }
    @RequestMapping("/deletegoods")
    @ResponseBody
    public String deleteGoods(@RequestBody Goods goods){
        goodsService.deleteGoods(goods);
        return "succ";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
     session.removeAttribute("adminuser");
     return "admin/login";
    }
    @RequestMapping("/activitylist")
    public String activity(){
        return "admin/activitylist";
    }
    @RequestMapping("/adminuserlist")
    public String adminUser(){
     return "admin/adminlist";
    }
    @RequestMapping("/userList")
    @ResponseBody
    public AdminUserPage adminUserList(int offset, int limit){
        AdminUserPage adminUserPage=new AdminUserPage();
        List<AdminUser> adminUserList=adminUserService.selectAdminUserListPage(offset,limit);
        int count=adminUserService.selectAdminUserPageCount();
        adminUserPage.setTotal(count);
        adminUserPage.setRows(adminUserList);
        return adminUserPage;
    }
    @RequestMapping("/useradd")
    @ResponseBody
    public String adminUserAdd(@RequestBody AdminUser adminUser){
        adminUser.setAdminUserAvatar("/images/avatar.png");
        adminUserService.adminUserAdd(adminUser);
        return "succ";
    }
    @RequestMapping("/deleteauser")
    @ResponseBody
    public String adminUserDelete(@RequestBody AdminUser adminUser){
        adminUserService.adminUserDelete(adminUser);
        return "succ";
    }
    @RequestMapping("/modifypassword")
    @ResponseBody
    public String modifyPassword(@RequestBody AdminUser adminUser){
        adminUserService.modifyPassword(adminUser);
        return "succ";
    }
}
